package com.frank.oj.controller.oj;

import com.frank.model.entity.discussion.Comment;
import com.frank.oj.annotation.AnonApi;
import com.frank.oj.common.result.CommonResult;
import com.frank.oj.model.dto.ReplyDTO;
import com.frank.oj.model.vo.CommentListVO;
import com.frank.oj.model.vo.CommentVO;
import com.frank.oj.model.vo.ReplyVO;
import com.frank.oj.service.oj.CommentService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 评论
 * @author frank
 * @Date 2024/4/15
 */
@RestController
@RequestMapping("/api")
public class CommentController {
    @Resource
    private CommentService commentService;

    /**
     * 评论获取
     * @param cid 比赛id
     * @param did 讨论id
     * @param limit 10
     * @param currentPage 1
     * @return 评论信息
     */
    @GetMapping("/comments")
    @AnonApi
    public CommonResult<CommentListVO> getComments(@RequestParam(value = "cid", required = false) Long cid,
                                                   @RequestParam(value = "did", required = false) Integer did,
                                                   @RequestParam(value = "limit", required = false, defaultValue = "20") Integer limit,
                                                   @RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage) {
        return commentService.getComments(cid, did, limit, currentPage);
    }

    /**
     * 评论添加
     */
    @PostMapping("/comment")
    @RequiresPermissions("comment_add")
    @RequiresAuthentication
    public CommonResult<CommentVO> addComment(@RequestBody Comment comment) {
        return commentService.addComment(comment);
    }

    /**
     * 评论删除
     */
    @DeleteMapping("/comment")
    @RequiresAuthentication
    public CommonResult<Void> deleteComment(@RequestBody Comment comment) {
        return commentService.deleteComment(comment);
    }

    /**
     * 评论点赞
     */
    @GetMapping("/comment-like")
    @RequiresAuthentication
    public CommonResult<Void> addCommentLike(@RequestParam("cid") Integer cid,
                                             @RequestParam("toLike") Boolean toLike,
                                             @RequestParam("sourceId") Integer sourceId,
                                             @RequestParam("sourceType") String sourceType) {
        return commentService.addCommentLike(cid, toLike, sourceId, sourceType);
    }

    /**
     * 全部回复
     */
    @GetMapping("/reply")
    @AnonApi
    public CommonResult<List<ReplyVO>> getAllReply(@RequestParam("commentId") Integer commentId,
                                                   @RequestParam(value = "cid", required = false) Long cid) {
        return commentService.getAllReply(commentId, cid);
    }

    /**
     * 回复评论
     */
    @PostMapping("/reply")
    @RequiresPermissions("reply_add")
    @RequiresAuthentication
    public CommonResult<ReplyVO> addReply(@RequestBody ReplyDTO replyDto) {
        return commentService.addReply(replyDto);
    }

    /**
     * 删除回复
     */
    @DeleteMapping("/reply")
    @RequiresAuthentication
    public CommonResult<Void> deleteReply(@RequestBody ReplyDTO replyDto) {
        return commentService.deleteReply(replyDto);
    }
}
